package com.example.ancientpoetryintelligencejava.controller;

import com.example.ancientpoetryintelligencejava.core.CoreProcessor;
import com.example.ancientpoetryintelligencejava.mapper.QuestionMapper;
import com.example.ancientpoetryintelligencejava.model.Data;
import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.seg.Segment;
import com.hankcs.hanlp.seg.common.Term;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @BelongsProject: ancient-poetry-intelligence-java
 * @BelongsPackage: com.example.ancientpoetryintelligencejava.controller
 * @Author: ZhShy
 * @CreateTime: 2022-03-15 19:33
 * @Description:
 */
@EnableAutoConfiguration
@Controller
@RequestMapping("question")
@CrossOrigin(origins = "*", maxAge = 3600)
public class QuestionController {
    @Autowired
    QuestionMapper questionMapper;


    @RequestMapping("/query")
    @ResponseBody
    public String query(@RequestParam(value = "question") String question) throws Exception {
        System.out.println(question);

        CoreProcessor query = new CoreProcessor("D:/data-for-1.7.5/data");
        List<String> reStrings = query.analysis(question);
        System.out.println(reStrings.size());
        Segment segment = HanLP.newSegment().enableCustomDictionary(true);
        List<Term> terms = segment.seg(question);
        for (Term term : terms) {
            System.out.println(term.toString());
        }
        int modelIndex = Integer.parseInt(reStrings.get(0));

        String answer = "";
        reStrings.set(1, terms.get(0).toString().split("/")[0]);
        System.out.println(reStrings.get(1));
        List<String> stringList = new ArrayList<>();
        /**匹配问题模板*/
        switch (modelIndex) {
            case 0:
//                诗词的作者
                stringList = questionMapper.poetOfPoetry_0(reStrings.get(1));

                for (String s : stringList) {
                    answer += "【";
                    answer += s;
                    answer += "】<br>";
                }
                break;
            case 1:
//                诗词的朝代
                stringList = questionMapper.dynastyOfPoetry_1(reStrings.get(1));
                if(stringList.size()==0){
                    stringList=questionMapper.dynastyOfPoet_3(reStrings.get(1));
                }
                for (String s : stringList) {
                    answer += "【";
                    answer += s;
                    answer += "】<br>";
                }

                break;
            case 2:
//                诗人的作品
                stringList = questionMapper.poetryListOfPoet_2(reStrings.get(1));
                if(stringList.size()==0){
                    stringList=questionMapper.poetryListOfDynasty_5(reStrings.get(1));
                }
                for (String s : stringList) {
                    answer += "【";
                    answer += s;
                    answer += "】<br>";
                }
                break;
            case 3:
//                诗人的朝代
                stringList = questionMapper.dynastyOfPoet_3(reStrings.get(1));
                for (String s : stringList) {
                    answer += "【";
                    answer += s;
                    answer += "】<br>";
                }
                break;
            case 4:
//                朝代的诗人
                stringList = questionMapper.poetListOfDynasty_4(reStrings.get(1));
                for (String s : stringList) {
                    answer += "【";
                    answer += s;
                    answer += "】<br>";
                }
                break;
            case 5:
//                朝代的作品
                stringList = questionMapper.poetryListOfDynasty_5(reStrings.get(1));
                for (String s : stringList) {
                    answer += "【";
                    answer += s;
                    answer += "】<br>";
                }
                break;
            case 6:
//                诗词的内容
                stringList = questionMapper.detailOfPoetry_6(reStrings.get(1));
                for (String s : stringList) {
                    answer += "【";
                    answer += s;
                    answer += "】<br>";
                }
                break;
            default:
                break;
        }
        System.out.println(answer);
        if (answer != null && !"".equals(answer.toString()) && !("\\N").equals(answer.toString())) {
            return answer.toString();
        } else {
            return "对不起，没有找到相关的答案";
        }
    }

    @RequestMapping("add")
    @ResponseBody
    public void add(@RequestParam String url) throws IOException {
//        String url = "https://so.gushiwen.cn/gushi/tangshi.aspx";
        Connection conn = Jsoup.connect(url).timeout(5000);
        conn.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
        conn.header("Accept-Encoding", "gzip, deflate, sdch");
        conn.header("Accept-Language", "zh-CN,zh;q=0.8");
        conn.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
        //所有在js中使用的方法，这里都能使用
//        Element element = document.getElementById("content");
        Element element = conn.get();
        //在获得网页内容后，获取所有的li标签
        Elements elements = element.getElementsByTag("span");
//            String url2 = "https://so.gushiwen.cn";
        int i = 1;
        for (Element el : elements) {
//            System.out.println(i);
//            i++;
            String url2 = "https://so.gushiwen.cn";
            String url2_ = el.getElementsByTag("a").attr("href");
            if (url2_ != "") {
                url2 += url2_;

                conn = Jsoup.connect(url2).timeout(5000);
                conn.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
                conn.header("Accept-Encoding", "gzip, deflate, sdch");
                conn.header("Accept-Language", "zh-CN,zh;q=0.8");
                conn.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
                Element element1 = conn.get().getElementById("sonsyuanwen");
                Elements source = element1.getElementsByClass("source").eq(0);

                String poetryTitle = element1.getElementsByTag("h1").eq(0).text();
                String poetName = source.get(0).getElementsByTag("a").eq(0).text();
                String dynastyName = source.get(0).getElementsByTag("a").eq(1).text().replace("〔", "").replace("〕", "");
                String poetryDetail = element1.getElementsByClass("contson").eq(0).text();

                System.out.println(poetryTitle + " " + poetName + " " + dynastyName + " " + poetryDetail);
                Integer dynastyId = questionMapper.dynastyId(dynastyName);
                Integer poetId = questionMapper.poetId(poetName);
                Integer poetryId = questionMapper.poetryId(poetryTitle, poetryDetail);

                if (dynastyId == null) {
                    questionMapper.insertIntoDynasty(dynastyName);
                    dynastyId = questionMapper.dynastyId(dynastyName);
                    questionMapper.insertIntoPoet(poetName);
                    poetId = questionMapper.poetId(poetName);
                    questionMapper.insettIntoPoetry(poetryTitle, poetryDetail);
                    poetryId = questionMapper.poetryId(poetryTitle, poetryDetail);
                    questionMapper.insertIntoPoet_Dynasty(dynastyId, poetId);
                    questionMapper.insertIntoPoetry_Dynasty(poetryId, dynastyId);
                    questionMapper.insertIntoPoetry_Poet(poetryId, poetId);
                }
                if (poetId == null) {
                    questionMapper.insertIntoPoet(poetName);
                    poetId = questionMapper.poetId(poetName);
                    questionMapper.insettIntoPoetry(poetryTitle, poetryDetail);
                    poetryId = questionMapper.poetryId(poetryTitle, poetryDetail);
                    questionMapper.insertIntoPoet_Dynasty(dynastyId, poetId);
                    questionMapper.insertIntoPoetry_Dynasty(poetryId, dynastyId);
                    questionMapper.insertIntoPoetry_Poet(poetryId, poetId);
                }
                if (poetryId == null) {
                    questionMapper.insettIntoPoetry(poetryTitle, poetryDetail);
                    poetryId = questionMapper.poetryId(poetryTitle, poetryDetail);
                    questionMapper.insertIntoPoet_Dynasty(dynastyId, poetId);
                    questionMapper.insertIntoPoetry_Dynasty(poetryId, dynastyId);
                    questionMapper.insertIntoPoetry_Poet(poetryId, poetId);
                }

//                System.out.println(poetryTitle+" "+poetName+" "+dynastyName+" "+poetryDetail);
            }
        }


    }

    @RequestMapping("printData")
    @ResponseBody
    public String printData() {
        String data = "";
//        String data="诗人.txt <br>";
        List<String> stringList = questionMapper.poetName();
        for (String s : stringList) {
            data += s;
            data += " nnt 100 <br>";
        }
        stringList = questionMapper.poetryTitle();
//        data+="诗词.txt <br>";
        for (String s : stringList) {
            data += s;
            data += " nm 100 <br>";
        }
        stringList = questionMapper.dynastyName();
//        data+="朝代.txt <br>";
        for (String s : stringList) {
            data += s;
            data += " ng 100 <br>";
        }
        return data;
    }


    @RequestMapping("getData")
    @ResponseBody
    public Data getData() {
        Data data = new Data();
        data.setDynastyList(questionMapper.selectDynasty());
        data.setPoet_dynastyList(questionMapper.selectPoet_Dynasty());
        data.setPoetList(questionMapper.selectPoet());
        data.setPoetry_dynastyList(questionMapper.selectPoetry_Dynasty());
        data.setPoetryList(questionMapper.selectPoetry());
        data.setPoetry_poetList(questionMapper.selectPoetry_Poet());
        return data;
    }

}
